import java.util.Map;
import java.util.HashMap;
import java.sql.ResultSet;

public class User{
    
    String userName;        //用户名
    String password_md5;        //密码
    String cardId;          //卡号
    long balance;         //余额
    
    static final String DEPOSIT_EXCEPTION_MSG = "存款失败: 存款额度不合法";
    static final String WITHDRAW_EXCEPTION_MSG = "取款失败: 取款额度不合法或余额不足";
    
    public User(String userName, String password, String cardId)
    throws Exception{
        try{
            //检查用户名是否合法
            checkUserName(userName);
            this.userName = userName;
            
            //检擦密码是否合法
            checkPassword(password);
            this.password_md5 = MD5.getMD5(password.getBytes()); 
            
            this.cardId = cardId;
            this.balance = 0;
        }catch(Exception e){
            throw e;
        }    
    }
    
    public User(String userName, String password, long balance, String cardId)
    throws Exception{
        try{
            this.userName = userName;          
            this.password_md5 = MD5.getMD5(password.getBytes());               
            this.balance = balance;
            this.cardId = cardId;
        }catch(Exception e){
            throw e;
        }
    }
    
    private void checkUserName(String userName)
    throws Exception{
        String userNameRegExp = "[0-9a-zA-Z\\_]+";
        if(!userName.matches(userNameRegExp)){
            throw new Exception("用户名错误：用户名只允许出现字母，数字，下划线，且长度至少为1");
        }
    }
    
    private void checkPassword(String password)
    throws Exception{
        String passwordRegExp = "[0-9a-zA-Z\\_]{6,20}";
        if(!password.matches(passwordRegExp)){
            throw new Exception("密码错误：密码只允许出现字母，数字，下划线，且密码长度为6-20");
        }
    }
    
    //存款，amount为存款额度
    public void deposit(long amount) 
    throws Exception{
        if(amount > 0){
            this.balance += amount;
        }else{
            //如果存款额度不合法，抛出异常
            throw new Exception(DEPOSIT_EXCEPTION_MSG);
        }
    }
    
    //取款方法，amount为取款额度
    public void withdraw(long amount)
    throws Exception{
        if(amount >= 0 && amount <= this.balance){
            balance -= amount;
        }else{
            //如果取款额度出现问题，抛出异常
            throw new Exception(WITHDRAW_EXCEPTION_MSG);
        }
    }
    
    //返回余额
    public long getBalance(){
        return this.balance;
    }
    
    public void setBalance(long b){
        this.balance = b;
    }
    
    // public void checkAcount(String userName, String password)
    // throws Exception{
    //     if(userName.equals(this.userName)){
    //         throw new Exception("错误: 用户名不正确");
    //     }
    //     if(password.equals(this.password_md5)){}
    // }
    
    public Map<String, Object> getMap(){
        Map<String, Object> userMap = new HashMap<String, Object>();
        userMap.put("username", "'" + this.userName + "'");
        userMap.put("password", "'" + this.password_md5 + "'");
        userMap.put("balance", this.balance);
        userMap.put("cardid", "'" + this.cardId + "'");
        
        return userMap;
    }
    
    public int hashCode(){
        return (int)((Long.parseLong(this.cardId))>>32);
    }
    
    public boolean equals(Object o){
        if(this == o){
            return true;
        }
        
        if(o.getClass() == User.class){
            User user = (User)o;
            if(user.cardId.equals(this.cardId)){
                return true;    
            }
        }
        
        return false;
    }
}